<template>
  <view class="wrap">
    <view class="content">
      <view class="title">用户注册</view>
      
      <!-- 用户名输入 -->
      <view class="form-item">
        <input 
          v-model="form.username" 
          type="text" 
          placeholder="请输入用户名" 
          class="input"
        />
      </view>
      
      <!-- 密码输入 -->
      <view class="form-item">
        <input 
          v-model="form.password" 
          type="password" 
          placeholder="请输入密码" 
          class="input"
        />
      </view>
      
      <!-- 确认密码输入 -->
      <view class="form-item">
        <input 
          v-model="form.confirmPassword" 
          type="password" 
          placeholder="请确认密码" 
          class="input"
        />
      </view>
      
      <!-- 注册按钮 -->
      <button 
        class="register-btn" 
        @click="register" 
        :loading="isLoading"
        :disabled="isLoading"
      >
        {{ isLoading ? '注册中...' : '注册' }}
      </button>
      
      <!-- 返回登录链接 -->
      <view class="login-text" @click="toLogin">
        已有账号，立即登录
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      form: {
        username: '',
        password: '',
        confirmPassword: ''
      },
      isLoading: false
    }
  },
  methods: {
    async register() {
      // 表单验证
      if (!this.form.username) {
        uni.showToast({
          title: '请输入用户名',
          icon: 'none'
        });
        return;
      }
      
      if (!this.form.password) {
        uni.showToast({
          title: '请输入密码',
          icon: 'none'
        });
        return;
      }
      
      if (this.form.password !== this.form.confirmPassword) {
        uni.showToast({
          title: '两次输入的密码不一致',
          icon: 'none'
        });
        return;
      }
      
      this.isLoading = true;
      
      try {
        const res = await uniCloud.callFunction({
          name: 'user',
          data: {
            action: 'register',
            username: this.form.username,
            password: this.form.password
          }
        });
        
        if (res.result.code === 200) {
          uni.showToast({
            title: '注册成功',
            icon: 'success',
            duration: 1500
          });
          
          // 延迟跳转到登录页面
          setTimeout(() => {
            uni.redirectTo({
              url: '/pages/login/login'
            });
          }, 1500);
        } else {
          uni.showToast({
            title: res.result.msg,
            icon: 'none',
            duration: 2000
          });
        }
      } catch (error) {
        console.error('注册错误:', error);
        uni.showToast({
          title: '注册失败，请稍后再试',
          icon: 'none',
          duration: 2000
        });
      } finally {
        this.isLoading = false;
      }
    },
    
    toLogin() {
      uni.redirectTo({
        url: '/pages/login/login'
      });
    }
  }
}
</script>

<style scoped>
.wrap {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f5f5;
  padding: 20rpx;
}

.content {
  width: 90%;
  max-width: 600rpx;
  padding: 50rpx 40rpx;
  background: #fff;
  border-radius: 24rpx;
  box-shadow: 0 10rpx 30rpx rgba(0, 0, 0, 0.15);
}

.title {
  font-size: 44rpx;
  font-weight: bold;
  text-align: center;
  margin-bottom: 60rpx;
  color: #333;
}

.form-item {
  margin-bottom: 40rpx;
}

.input {
  height: 90rpx;
  padding: 0 30rpx;
  border: 2rpx solid #e0e0e0;
  border-radius: 12rpx;
  font-size: 32rpx;
  transition: border-color 0.3s;
}

.input:focus {
  background-color: #2979ff;
  outline: none;
}

.register-btn {
  height: 90rpx;
  line-height: 90rpx;
  border-radius: 12rpx;
  background-color: #2979ff;
  color: #fff;
  font-size: 34rpx;
  font-weight: bold;
  margin-top: 20rpx;
  border: none;
}

.register-btn[disabled] {
  opacity: 0.7;
}

.login-text {
  text-align: center;
  margin-top: 40rpx;
  font-size: 30rpx;
  color: #2575fc;
  text-decoration: underline;
  cursor: pointer;
}
</style>